package com.songcha.module_home.ui.fragment.homehome

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreViewModel
import com.songcha.library_business.bean.banner.BannerBean
import com.songcha.library_business.bean.book.BookListBean
import com.songcha.library_business.proxy.personal_recommend.PersonalRecommendManager
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_home.bean.HomeHomeBean
import com.songcha.module_home.ui.adapter.HomeMultiItemType
import io.reactivex.Observable
import io.reactivex.functions.Function5

class HomeHomeViewModel(app: Application):BaseRefreshLoadMoreViewModel<HomeHomeRepository,HomeMultiItemType>(app) {
    //val homeHomeList=MutableLiveData<MutableList<HomeMultiItemType>>()
    val editorRecommendState=MutableLiveData<Boolean>()
    var editorRecommendCurPage=1
    var getHomeHomeDataState=MutableLiveData<Boolean>()

    fun getEditorRecommend(){
        //TODO
        editorRecommendCurPage++
        handleApiDataObserver(repository!!.getEditorRecommendBookList(1,100),object :IApiResult<BookListBean>{
            override fun onSuccess(bean: BookListBean) {
                if(rlst.value!=null && rlst.value!!.size == 5){
                    if(bean.data.records.size>0){
                        bean.data.records.shuffle()
                        bean.data.records = bean.data.records.subList(0,6)

                        val majiaName = MajiaBaoUtil.getMajiaName()
                        if(majiaName == "douyue"){
                            rlst.value!![1]=HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_EDITOR_RECOMMEND,bean)
                        }else{
                            rlst.value!![rlst.value!!.size-1]=HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_EDITOR_RECOMMEND,bean)
                        }
                        editorRecommendState.value=true
                    }else{
                        editorRecommendCurPage--
                        editorRecommendState.value=false
                    }
                }
            }

            override fun onError(e: Throwable) {
                editorRecommendCurPage--
                editorRecommendState.value=false
            }
        })
    }

    fun getHostBookList(){
        if(rlst.value==null || rlst.value!!.size==0) return
        val oldList=(rlst.value!![rlst.value!!.size-1].data as BookListBean).data.records
        handleApiDataObserver(repository!!.getHostBookList(currentPage),object :IApiResult<BookListBean>{
            override fun onSuccess(bean: BookListBean) {
                if(bean.data.records.size==0){
                    onLoadMoreNoData()
                }else{
                    val start=oldList.size
                    val newList=bean.data.records
                    val size=newList.size
                    oldList.addAll(newList)
                    onLoadMoreSuccess(start,size)
                }
            }

            override fun onError(e: Throwable) {

            }

        },true)
    }

    fun getHomeHomeData(){
        val obsList = mutableListOf<Observable<out BaseBean>>()
        obsList.add(repository.getBanner(1))
        if(PersonalRecommendManager.getPersonalRecommendEnabled()){
            //TODO
            obsList.add(repository.getEditorRecommendBookList(1,100))
        }
        obsList.add(repository.getBanner(2))
        var newBookShelvePageSize = 5
        val majiaName = MajiaBaoUtil.getMajiaName()
        if(majiaName == "beike")
            newBookShelvePageSize = 6
        obsList.add(repository.getNewBookShelveList(1,newBookShelvePageSize))
        obsList.add(repository.getHostBookList(1))

        val obsZip = Observable.zip(obsList,object : io.reactivex.functions.Function<Array<Any>,HomeHomeBean>{
            override fun apply(t: Array<Any>): HomeHomeBean {
                val list = mutableListOf<HomeMultiItemType>()
                //personal recommend maybe off
                if(t.size == 4){
                    val t1 = t[0] as BannerBean
                    val t3 = t[1] as BannerBean
                    val t4 = t[2] as BookListBean
                    val t5 = t[3] as BookListBean
                    if(majiaName == "douyue"){
                        if(t1.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_TOP_BANNER,t1))
                        //list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_ADVERT_XXL,t1))
                        if(t3.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_MIDDLE_BANNER,t3))
                        if(t4.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_NEWBOOK_SHELVE,t4))
                        if(t5.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_HOT_LSIT,t5))
                    }else{
                        if(t1.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_TOP_BANNER,t1))
                        if(t5.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_HOT_LSIT,t5))
                        //if(t3.data.size>0)
                            //list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_MIDDLE_BANNER,t3))
                        if(t4.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_NEWBOOK_SHELVE,t4))
                    }

                }else if(t.size == 5){
                    val t1 = t[0] as BannerBean
                    val t2 = t[1] as BookListBean
                    val t3 = t[2] as BannerBean
                    val t4 = t[3] as BookListBean
                    val t5 = t[4] as BookListBean

                    if(t2.data.records.size>0){
                        t2.data.records.shuffle()
                        t2.data.records = t2.data.records.subList(0,6)
                    }

                    if(majiaName == "douyue"){
                        if(t1.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_TOP_BANNER,t1))
                        if(t2.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_EDITOR_RECOMMEND,t2))
                        if(t3.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_MIDDLE_BANNER,t3))
                        //list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_ADVERT_XXL,t1))
                        if(t4.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_NEWBOOK_SHELVE,t4))
                        if(t5.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_HOT_LSIT,t5))
                    }else{
                        if(t1.data.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_TOP_BANNER,t1))
                        if(t5.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_HOT_LSIT,t5))
                        //if(t3.data.size>0)
                            //list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_MIDDLE_BANNER,t3))
                        //list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_ADVERT_XXL,t1))
                        if(t4.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_NEWBOOK_SHELVE,t4))
                        if(t2.data.records.size>0)
                            list.add(HomeMultiItemType(HomeMultiItemType.HOME_MULTIITEMTYPE_EDITOR_RECOMMEND,t2))
                    }

                }

                val bean=HomeHomeBean(list)
                return bean
            }
        })

        handleApiDataObserver(obsZip,object :IApiResult<HomeHomeBean>{
            override fun onSuccess(bean: HomeHomeBean) {
                handleDataListSuccess(bean.data)
                getHomeHomeDataState.value=true
            }

            override fun onError(e: Throwable) {
                getHomeHomeDataState.value=false
            }

        },true)

    }
}